<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Config;
use fast\Tree;
use think\Session;
use think\Validate;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function _initialize()
    {
        parent::_initialize();

        $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);

        $this->assign('office',Config::get('site.office'));
        $this->assign('office_addr',Config::get('site.office_addr'));
        $this->assign('office_email',Config::get('site.office_email'));
        $this->assign('office_tel',Config::get('site.office_tel'));
        $this->assign('factory_tel',Config::get('site.factory_tel'));
        $this->assign('beian',Config::get('site.beian'));
        $this->assign('copyright',Config::get('site.copyright'));
        $this->assign('logo',Config::get('site.logo'));
        $this->assign('site_name',Config::get('site.name'));
        $this->assign('domain',Config::get('site.domain'));
        $this->assign('footer_bg',Config::get('site.footer_bg'));
        $this->assign('app_code',Config::get('site.app_code'));
        $this->assign('wechat_code',Config::get('site.wechat_code'));

        $this->assign('contact_icon_1',Config::get('site.contact_icon_1'));
        $this->assign('contact_icon_1_link',Config::get('site.contact_icon_1_link'));
        $this->assign('contact_icon_2',Config::get('site.contact_icon_2'));
        $this->assign('contact_icon_2_link',Config::get('site.contact_icon_2_link'));
        $this->assign('contact_icon_3',Config::get('site.contact_icon_3'));
        $this->assign('contact_icon_3_link',Config::get('site.contact_icon_3_link'));
        $this->assign('contact_icon_4',Config::get('site.contact_icon_4'));
        $this->assign('contact_icon_4_link',Config::get('site.contact_icon_4_link'));
        $this->assign('contact_icon_5',Config::get('site.contact_icon_5'));
        $this->assign('contact_icon_5_link',Config::get('site.contact_icon_5_link'));

        $lanmu = Db::name('lanmu')->where('switch',1)->order('weigh desc,id desc')->select();
        $this->assign('lanmu',$lanmu);

        $this_action_name = request()->action();
        if($this_action_name=='product_show'){
            $this_action_name = 'products';
        }
        if($this_action_name=='application_show'){
            $this_action_name = 'application';
        }
        if($this_action_name=='service_show'){
            $this_action_name = 'service';
        }
        $this_action = Db::name('lanmu')->where('keyword',$this_action_name)->find();
        $this->assign('this_action',$this_action);

        $category = Db::name('category')->where(['status'=>'normal','pid'=>0])->order('weigh desc,id desc')->select();
        foreach ($category as &$item) {
            $son = Db::name('category')->where(['status'=>'normal','pid'=>$item['id']])->order('weigh desc,id desc')->select();
            foreach ($son as &$vo) {
                $vo['son'] = Db::name('category')->where(['status'=>'normal','pid'=>$vo['id']])->order('weigh desc,id desc')->select();
            }
            $item['son'] = $son;
        }
        $this->assign('category',$category);

        $where = [
            'switch' => 1,
            'deletetime' => null
        ];
        $app_left = Db::name('app')->where($where)->order('weigh desc,id desc')->select();
        $this->assign('app_left',$app_left);
        $this->assign('appid',input('appid/d',''));

        $service_left = Db::name('servicecate')->order('weigh desc,id desc')->select();
        $this->assign('service_left',$service_left);
        $this->assign('sid',input('sid/d',''));
    }

    public function index()
    {
        $banner = Db::name('banner')->where(['deletetime'=>null,'switch'=>1])->order('weigh desc,id desc')->select();
        $this->assign('banner',$banner);

        $this->assign('index_short_desc',Config::get('site.index_short_desc'));
        $this->assign('about_us_image',Config::get('site.about_us_image'));

        $where = [
            'switch' => 1,
            'deletetime' => null,
            'state' => 1
        ];
        $top_products = Db::name('product')->where($where)->limit(0,4   )->order('weigh desc,id desc')->select();
        $this->assign('top_products',$top_products);

        $where = [
            'switch' => 1,
            'deletetime' => null,
            'state' => 2
        ];
        $middle_products = Db::name('product')->where($where)->order('weigh desc,id desc')->select();
        $this->assign('middle_products',$middle_products);

        $where = [
            'switch' => 1,
            'deletetime' => null,
            'state' => 1
        ];
        $service_cate = Db::name('servicecate')->order('weigh desc,id desc')->select();
        foreach ($service_cate as &$cate_item) {
            $where['cid'] = $cate_item['id'];
            $service = Db::name('service')->where($where)->limit(0,4)->order('weigh desc,id desc')->select();
            if($service){
                foreach ($service as &$item) {
                    $item['heading'] = date('d.M.Y',strtotime($item['heading']));
                }
                $service[0]['date'] = date('d',strtotime($service[0]['heading']));;
                $service[0]['month'] = date('M',strtotime($service[0]['heading']));;
                $service[0]['year'] = date('Y',strtotime($service[0]['heading']));;
            }
            $cate_item['son'] = $service;
        }

        $this->assign('service_cate',$service_cate);

        return $this->view->fetch();
    }

    public function about()
    {
        $this->assign('about_us_title',Config::get('site.about_us_title'));
        $this->assign('about_us_content',Config::get('site.about_us_content'));
        $this->assign('about_us_image',Config::get('site.about_us_image'));
        return $this->view->fetch();
    }

    //产品咨询
    public function product_mess()
    {
        $params = $this->request->post();
        Db::name('productmsg')->insert([
            'productid' => $params['id'],
            'contact' => xykeepFilterSql($params['contact']),
            'createtime' => time(),
            'updatetime' => time()
        ]);
        $this->success('提交成功');
    }

    public function products()
    {
        $pid = input('pid/d','');

        $keyword = input('keyword/s','');
        $where = [
            'state' => ['neq',1],
            'switch' => 1,
            'deletetime' => null
        ];
        $where2 = [];
        if($keyword!==''){
            $keyword = xykeepFilterSql($keyword);
            $where['title'] = ['like','%'.$keyword.'%'];
            $this->assign('pid','');
        }else{
            //如果是表单过来的就没有分类筛选
            if(!$pid){
                $pid = Db::name('category')->where(['status'=>'normal','pid'=>0])->order('weigh desc,id desc')->value('id');
            }
            $category_ids = $pid;
            $sonCateList = Db::name('category')->where(['pid'=>$pid])->select();
            foreach ($sonCateList as $item) {
                $category_ids .= ','.$item['id'];
                $sonSonCateList = Db::name('category')->where(['pid'=>$item['id']])->select();
                foreach ($sonSonCateList as $sitem) {
                    $category_ids .= ','.$sitem['id'];
                }
            }
            $where2[] = ['exp',Db::raw("find_in_set(category_id,'".$category_ids."')")];

            //去找到最顶级的分类
            $pidInfo = Db::name('category')->where('id',$pid)->find();
            while ($pidInfo['pid']!==0){
                $pidInfo = Db::name('category')->where('id',$pidInfo['pid'])->find();
            }
            $this->assign('pid',$pidInfo['id']);
        }

        $product = Db::name('product')->where($where)->where($where2)->order('weigh desc,id desc')->paginate(12);
        $pages = $product->render();
        $this->assign('product',$product);
        $this->assign('pages',$pages);


        return $this->view->fetch();
    }

    public function product_show()
    {
        $pid = input('pid/d','');
        $this->assign('pid',$pid);
        $id = input('id/d','');
        $where = [
            'switch' => 1,
            'deletetime' => null
        ];
        if($id){
            $where['id'] = $id;
        }else{
            return;
        }
        $product = Db::name('product')->where($where)->find();
        $product['images'] = explode(',',$product['images']);
        $this->assign('product',$product);

        $next_product_id = Db::name('product')->where([
            'switch' => 1,
            'deletetime' => null,
            'id' => ['gt',$product['id']]
        ])->order('id asc')->value('id');

        $prev_product_id = Db::name('product')->where([
            'switch' => 1,
            'deletetime' => null,
            'id' => ['lt',$product['id']]
        ])->order('weigh desc,id desc')->value('id');

        $this->assign('next_product_id',$next_product_id);
        $this->assign('prev_product_id',$prev_product_id);

        //去找到最顶级的分类
        $pidInfo = Db::name('category')->where('id',$product['category_id'])->find();
        while ($pidInfo['pid']!==0){
            $pidInfo = Db::name('category')->where('id',$pidInfo['pid'])->find();
        }
        $this->assign('pid',$pidInfo['id']);

        return $this->view->fetch();
    }

    public function application()
    {
        $where = [
            'switch' => 1,
            'deletetime' => null
        ];
        $app = Db::name('app')->where($where)->order('weigh desc,id desc')->select();
        $this->assign('app',$app);
        return $this->view->fetch();
    }

    public function application_show()
    {
        $id = input('appid/d','');
        $where = [
            'switch' => 1,
            'deletetime' => null
        ];
        if($id){
            $where['id'] = $id;
        }else{
            return;
        }
        $application = Db::name('app')->where($where)->find();
        $this->assign('application',$application);
        return $this->view->fetch();
    }

    public function service()
    {
        $where = [
            'switch' => 1,
            'deletetime' => null
        ];
        if($sid = input('sid/d','')){
            $where['cid'] = $sid;
        }else{
            $sid = Db::name('servicecate')->order('weigh desc,id desc')->value('id');
            $where['cid'] = $sid;
            $this->assign('sid',$sid);
        }
        $service = Db::name('service')->where($where)->order('weigh desc,id desc')->paginate(5);
        foreach ($service as &$item) {
            $item['heading'] = date('M.d.Y',strtotime($item['heading']));
        }
        $pages = $service->render();
        $this->assign('service',$service);
        $this->assign('pages',$pages);
        return $this->view->fetch();
    }

    public function service_show()
    {
        $id = input('id/d','');
        $where = [
            'switch' => 1,
            'deletetime' => null
        ];
        if($id){
            $where['id'] = $id;
        }else{
            return;
        }
        $service = Db::name('service')->where($where)->find();
        $this->assign('sid',$service['cid']);
        $this->assign('service',$service);
        return $this->view->fetch();
    }

    public function contact()
    {

        $this->assign('factory',Config::get('site.factory'));
        $this->assign('factory_addr',Config::get('site.factory_addr'));
        $this->assign('factory_email',Config::get('site.factory_email'));
        $this->assign('factory_tel',Config::get('site.factory_tel'));
        $this->assign('contact_ad',Config::get('site.contact_ad'));
        $this->assign('contact_form_bg',Config::get('site.contact_form_bg'));
        return $this->view->fetch();
    }

    public function icontact()
    {
        $params = $this->request->post();

        if($params['username']==''){
            $this->error('Please fill in your name');
        }
        if($params['email']==''){
            $this->error('Please fill in your email');
        }
        if (!Validate::is($params['email'], "email")) {
            $this->error('The mailbox format is incorrect');
        }
        if($params['question']==''){
            $this->error('Please fill in your message');
        }

        $insert = [];
        $insert['username'] = xykeepFilterSql($params['username']);
        $insert['email'] = xykeepFilterSql($params['email']);
        $insert['mobile'] = xykeepFilterSql($params['tel']);
        $insert['contact'] = xykeepFilterSql($params['question']);
        $insert['createtime'] = $insert['updatetime'] = time();
        $res = Db::name('icontact')->insert($insert);
        if($res){
            $this->success('success');
        }else{
            $this->error('fail');
        }

    }
}
